import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from '../user/entities/user.entity';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisService } from '../redis/redis.service';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './auth.guard';
import { RedisModule } from '../redis/redis.module';
import { createClient } from 'redis'; // 导入 ConfigModule 和 ConfigService

@Module({
  imports: [
    JwtModule.registerAsync({
      imports: [ConfigModule], // 引入 ConfigModule
      useFactory: async () => ({
        secret: 'zjj', // 从环境变量中获取密钥
        signOptions: { expiresIn: '60s' }, // 可选的配置，如过期时间
      }),
      inject: [ConfigService], // 注入 ConfigService
    }),
    RedisModule,
    TypeOrmModule.forFeature([User]), // 导入用户实体
  ],
  controllers: [AuthController],
  providers: [
    AuthService,
    JwtService,
    RedisService,
    {
      provide: 'REDIS_CLIENT',
      async useFactory() {
        const client = createClient({
          socket: {
            host: 'localhost',
            port: 6379,
          },
          database: 0,
        });
        await client.connect();
        return client;
      },
    },
  ],
  exports: [AuthService], // 导出 AuthService 以供其他模块使用
})
export class AuthModule {}
